package excel;

import constant.ClassType;
import constant.Common;
import constant.Regex;
import util.DateUtil;
import util.StringUtil;
import util.lambda.Empty;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;

/**
 * excel工具类
 *
 * @author zhanglinfeng
 * @date create in 2023/6/5 10:20
 */
public class ExcelHeaderVO {
    /** 表头名称 */
    private String name;
    /** 表头对应的字段 */
    private String columnName;
    /** 表头对应的字段类型 */
    private String columnType;

    public ExcelHeaderVO() {
    }

    public ExcelHeaderVO(String name, String columnName, String columnType) {
        this.name = name;
        this.columnName = columnName;
        this.columnType = columnType;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColumnName() {
        return columnName;
    }

    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    public String getColumnType() {
        return columnType;
    }

    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    public String getColumnValue(Object data) {
        if (null == data) {
            return Common.BLANK;
        }
        if (ClassType.DECIMAL_TYPE_LIST.contains(data.getClass().getSimpleName())) {
            if (StringUtil.isNotEmpty(columnType) && columnType.matches(Regex.POSITIVE_INTEGER)) {
                return new BigDecimal(data.toString()).setScale(Integer.parseInt(columnType), RoundingMode.HALF_UP).toString();
            }
            return new BigDecimal(data.toString()).stripTrailingZeros().toPlainString();
        }
        if (data instanceof LocalDateTime) {
            return DateUtil.format((LocalDateTime) data, Empty.of(columnType).orElse(DateUtil.YYYY_MM_DD_HH_MM_SS));
        }
        if (data instanceof LocalDate) {
            return DateUtil.format((LocalDate) data, Empty.of(columnType).orElse(DateUtil.YYYY_MM_DD));
        }
        if (data instanceof LocalTime) {
            return DateUtil.format((LocalTime) data, Empty.of(columnType).orElse(DateUtil.HH_MM_SS));
        }
        if (data instanceof Timestamp) {
            Timestamp timestamp = (Timestamp) data;
            return DateUtil.format(timestamp.toLocalDateTime(), Empty.of(columnType).orElse(DateUtil.YYYY_MM_DD_HH_MM_SS));
        }
        if (data instanceof Time) {
            Time time = (Time) data;
            return DateUtil.format(time.toLocalTime(), Empty.of(columnType).orElse(DateUtil.HH_MM_SS));
        }
        if (data instanceof Date) {
            return DateUtil.format((Date) data, Empty.of(columnType).orElse(DateUtil.YYYY_MM_DD_HH_MM_SS));
        }
        return StringUtil.toString(data);
    }

}
